package org.krishna.Dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.krishna.model.Login;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/*@Service("coreService")*/
@Service("saveData")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class SaveDataImpl implements SaveData{
	
	private static final Logger logger = LoggerFactory.getLogger(SaveDataImpl.class);
	
	@Autowired
    private SessionFactory sessionFactory;

	@Override
	public void registerUser(Login login) {
		
		this.sessionFactory.getCurrentSession().save(login);
		logger.info("saved the details for registration");
			
	}

	@Override
	public List<Login> getParents() {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Login.class);
		criteria.add(Restrictions.eq("type", "parent"));
		List<Login> parents = criteria.list();
		if (parents != null && parents.size() >= 1) {
			return parents;
		}else{
			return null;
		}
		
	}

	/*private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}*/
	
	/*@Override	
	public void addTeam(Team team) {
		logger.info("Team is "+team);
		this.sessionFactory.getCurrentSession().save(team);
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Team> getTeams() {
	//return this.sessionFactory.getCurrentSession().createQuery("from Team").list();
	return (List<Team>)this.sessionFactory.getCurrentSession().createQuery("from Team").list();
	}
	
	@Override
	public void addStudent(Student student) {
		this.sessionFactory.getCurrentSession().save(student);		
	}
	
	@Override
	public Student getStudentByFirstName(String firstName) {
		//Session session = this.sessionFactory.getCurrentSession();
		//org.hibernate.Query quer = session.createQuery("abc");
		org.hibernate.Query query = this.sessionFactory.getCurrentSession().createQuery("from com.journaldev.model.Student as s where s.firstName = :firstname");
		query.setParameter("firstname", firstName);
		List<Student> list = query.list();
		if (list != null && list.size() >= 1) {
			return (Student) list.get(0);
		}else{
			return null;
		}
	}

	@Override
	public void addMarks(MarksDetails marksDetails) {
		this.sessionFactory.getCurrentSession().save(marksDetails);	
		
	}
	//Used criteria for retrieval of Data
	@Override
	public Student getStudentByName(String firstName) {
		Session session = this.sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Student.class);
		criteria.add(Restrictions.eq("firstName", firstName));
		List<Student> list = criteria.list();
		if (list != null && list.size() >= 1) {
			return (Student) list.get(0);
		}else{
			return null;
		}		
	}*/
	
	
	
	

}
